/**
 *  *2010-3-12 下午04:05:31  add 
 */
package com.base.myproject.client.ui;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.ColumnInfo;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.Debug;
import com.base.myproject.client.ui.codenamemap.CodeNameMapFactory;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.StoreFilterField;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;

/**
 * 基本的辅助录入对话框，按钮触发形式
 * @author Administrator
 *2010-3-12
 * javazgw@gmail.com
 */
/*
 		
			 
			
			 List<ColumnConfig> bocodecolumns2 = new ArrayList<ColumnConfig>();
				ColumnConfig bcode2 = new ColumnConfig("code", "人员", 100);

				bocodecolumns2.add(bcode2);

				ColumnConfig bname = new ColumnConfig("name", "人员名", 100);
				bocodecolumns2.add(bname);
				
				ModelType type = new ModelType();
				type.setRoot("records");
				type.setRecordName("record");
				type.setTotalName("totle");
				type.addField("code");
				type.addField("name");
				StoreInfo info = new StoreInfo();
				info.setTablename("bcode");
				info.setNeedAllRow("no");
				info.setSql("select bcode as code ,bname as name from bcode where bcode is not null and bname is not null and canchangepo=1");
		
			 BaseInputDialog bd =new BaseInputDialog(bocodecolumns2, type,info, "name", "code"){
				 public  void doposted(List<BusiObject> at) {
						for(BusiObject bo:at)
						{
								System.out.println("code="+bo.get("code"));
						}
						
					}
			 };
			 
			 bd.show();
 */
public class BaseInputDialog {
	boolean b;
	final Window w = new Window();
	List<ColumnConfig> columns = null;
	ModelType type = null;
	StoreInfo info = null;
	String returncolumn = null;
	BaseGrid<BusiObject> grid;
	BaseStore<BusiObject> basestore;
	BaseStore<BusiObject> target_store;
	String code;
	ColumnInfo[] sourcecol;
	ColumnInfo[] target_col;
	final Button yesbutton = new Button("确定");
	final Button button = new Button("取消");
	Button reloadbutton = new Button("刷新");
	StoreFilterField<BusiObject> field ;
	
	
	public BaseInputDialog(List<ColumnConfig> columns, ModelType type,
			StoreInfo info, String returncolumn,String code)
	{
		
		this.columns = columns;
		this.type = type;
		this.info = info;
		this.code = code;
		this.returncolumn = returncolumn;
		init();
	}
	public BaseInputDialog(List<ColumnConfig> columns,BaseStore<BusiObject> basestore,String returncolumn) {
		this(columns,basestore,returncolumn,null);
	}
	public BaseInputDialog(List<ColumnConfig> columns,BaseStore basestore,String returncolumn,String code) {
		super();
		
		this.columns = columns;

		this.returncolumn = returncolumn;
		this.code = code;
		this.basestore = basestore;
		
		init();
	}
	private void init() {
		
		
	}
	public void show()
	{
		
		if (!b) {
			initGrid();
			w.show();
			b = true;
		} else
			w.show();
	}

	private void initGrid() {
		w.setHeading("选择");
		w.setModal(true);
		w.setSize(400, 480);
		w.setMaximizable(true);
		final CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		columns.add(0, cbs.getColumn());
		ColumnModel cm = new ColumnModel(columns);
		if(type!=null)
			basestore = new BaseStore(type, info);
		
			grid = new BaseGrid(basestore, cm,false,false,null);
			grid.setAutoWidth(true);
			grid.setAutoHeight(true);
			cbs.setSelectionMode(SelectionMode.MULTI);
			grid.setSelectionModel(cbs);
			
			ContentPanel bottompanel = new ContentPanel();
			bottompanel.setHeaderVisible(false);
			bottompanel.setLayout(new TableLayout(2));
			yesbutton.addListener(Events.Select, getListener());
			button.addListener(Events.Select, getListener());
			reloadbutton.addListener(Events.Select, getListener());
			bottompanel.add(yesbutton);
			bottompanel.add(button);
			grid.setLoadMask(true);
			ContentPanel c = new ContentPanel();
			c.setHeaderVisible(false);
			//--加入过滤查找
			field = new StoreFilterField<BusiObject>() {
				@Override
				protected boolean doSelect(Store<BusiObject> store,
						BusiObject parent, BusiObject record, String property,
						String filter) {

				return	Select(store,parent,record,property,filter);
				}

				@Override
				protected void onFilter() {
					super.onFilter();

				
				}

			};
			
			field.setAutoWidth(true);
			field.setToolTip("对当前页面进行查找");
			field.bind(basestore);
			field.setFieldLabel("当页搜索");
			//--
			c.setLayout(new RowLayout(Orientation.VERTICAL));  
			ContentPanel cpanel = new ContentPanel();
			cpanel.setHeaderVisible(false);
			cpanel.setLayout(new RowLayout(Orientation.HORIZONTAL));
			reloadbutton.setToolTip("什么时候用此按钮？当你要选择的数据在别处被改变，点击刷新保持数据最新");
			if(this.type!=null)
			cpanel.add(field,new RowData(1, 1, new Margins(4)));
			
			cpanel.add(reloadbutton,new RowData(-1, 1, new Margins(4)));
			cpanel.setHeight(30);
			
			c.add(cpanel,new RowData(1, -1, new Margins(4)));
			c.add(grid,new RowData(1, 1, new Margins(4)));
			
			w.setScrollMode(Scroll.AUTO);
			w.add(c);
			w.addButton(yesbutton);
			w.addButton(button);
			// w.setBottomComponent(bottompanel);
			w.addListener(Events.Hide, getListener());
			w.show();
	}
	int sync = 0;                                                      
	Listener<BaseEvent> defaultlistener;                               
                                                                     
	Listener<BaseEvent> getListener() {                                
		if(defaultlistener!=null)                                        
		{                                                                
			return defaultlistener;                                        
		}                                                                
                                                                     
		defaultlistener = new Listener<BaseEvent>() {                    
			public void handleEvent(BaseEvent be) {                        
				sync++;                                                      
				try {                                                        
					if (sync == 1) {                                           
						if (be.getType() == Events.Select) {                     
							if (be.getSource().equals(yesbutton)) {   
								
								doposted(grid.getSelectionModel().getSelectedItems());
                                                                     
							}       
							else if (be.getSource().equals(button)) {   
								close();
								
                                
							}   
							else if(be.getSource() == reloadbutton)
							{
								field.reset();
								basestore.reload();
							}
						}
						else if (be.getType() == Events.Hide) {    
							
						}
						else if (be.getType() == Events.Change) {              
							if (be.getSource().equals("")) {         
                                                                     
							}                                                      
						} else if (be.getType() == Events.KeyUp) {               
							if (be instanceof ComponentEvent) {                    
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{                                                    
								}                                                    
							}                                                      
						}                                                        
					}                                                          
				} finally {                                                  
					sync--;                                                    
				}                                                            
			}

		                                                       
                                                                     
		};                                                               
		return defaultlistener;                                          
	}     
	/**
	 * 过滤方法，有特殊的方式之类可以重载
	 * @param store
	 * @param parent
	 * @param record
	 * @param property
	 * @param filter
	 * @return
	 */
		protected boolean Select(Store<BusiObject> store,
				BusiObject parent, BusiObject record, String property,
				String filter) {
			for (int i = 0; i < type.getFieldCount(); i++) {

				Object name = record.get(type.getField(i).getName());
				
				//TODO
				if (name!=null&&(name.toString().indexOf(filter.toLowerCase()) != -1 || name.toString().indexOf(filter.toUpperCase()) != -1 )) {
					return true;
				}
			}
			return false;
		}
		
		/**
		 * 提交后处理，之类继承
		 * @param at
		 */
		public  void doposted(List<BusiObject> at) {
			
			
		}
		protected void close() {
			w.hide();
			
		}       
}
